<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use think\Controller;
use think\Request;
use think\Session;

class Check extends Controller
{

    protected function _initialize()
    {
        $blacklist = ['login'];
        if (in_array($this->request->action(),$blacklist)){
            Admin::check_login() AND $this->redirect('index/index');
        }
    }

    /**
     * 登录
     *
     * @return \think\Response
     */
    public function login(Request $request)
    {
        $request->isPost() AND $this->checks();
        return $this->fetch();
    }

    /**
     * 登录POST验证.
     *
     * @return \think\Response
     */
    private function checks()
    {
        $result = Admin::checkLogin();
        if ($result['error']){
            $this->error($result['message']);
        }
        $this->success($result['message'],url('index/index'));
    }

    /**
     * 注销用户
     * @return \think\Response
     */
    public function signout()
    {
        // 判断有无登录 如无登录则不做任何操作
        Admin::check_login()  AND session(null);
        $this->success('已退出~',url('check/login'));
    }

}
